package com.example.android2019.ch3;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.android2019.R;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Ch3FileInnerActivity extends AppCompatActivity {
    Button btn1, btn2;
    TextView tv;
    String fileNme = "hello.txt";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ch3_file_inner);
          /*内部保存*/
        setContentView(R.layout.activity_ch3_file_inner);
        /*关联控件*/
        btn1 = findViewById(R.id.ch3_file_inner_btn_write);
        btn2 = findViewById(R.id.ch3_file_inner_btn_read);
        tv = findViewById(R.id.ch3_file_inner_tv);
        /*按钮点击事件及绑定*/
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.ch3_file_inner_btn_write:
                        write();
                        break;
                    case R.id.ch3_file_inner_btn_read:
                        read();
                        break;
                }
            }
        };
        btn1.setOnClickListener(listener);
        btn2.setOnClickListener(listener);

        /*获取传递的数据*/
        Intent intent = this.getIntent();
        String a = intent.getStringExtra("test");
        Toast.makeText(this, a, Toast.LENGTH_SHORT).show();
    }

    private void read() {
        FileInputStream in = null;
        BufferedReader br = null;
        try {
            in = this.openFileInput(fileNme);
            br = new BufferedReader(new InputStreamReader(in));
            String line = null;
            String s = "";
            while ((line = br.readLine()) != null) {
                s = s + line + "\n";
            }
            tv.setText(s);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (br != null) {
                try {
                    br.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void write() {
        String message = "hello\n中国";
        FileOutputStream out = null;
        BufferedWriter bw = null;
        try {
            out = this.openFileOutput(fileNme, MODE_PRIVATE);
            bw = new BufferedWriter(new OutputStreamWriter(out));
            bw.write(message);
            bw.flush();
            Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (bw != null) {
                try {
                    bw.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
